//
//  LZRestaurantAuthPresenter.m
//  laziz_Merchant
//
//  Created by xbnzd on 17/3/7.
//  Copyright © 2017年 XBN. All rights reserved.
//

#import "LZRestaurantAuthPresenter.h"
#import "NetworkServiceManager.h"
#import "NSString+Additions.h"
#import "MJExtension.h"

#import "LZRestaurantAuthModel.h"
#import "LZRestaurantAreaModel.h"
#import "LZRestaurantCuisineModel.h"
#import "LZPopSelectCollectViewModel.h"
#import "LZUserModel.h"

extern NSString *merchant;
static NSString *const kResAuthCuisineURL = @"/merchant/listCookingStyle"; //菜系
static NSString *const kResAuthAreaURL = @"/merchant/listMerchantArea"; //商区
static NSString *const kResAuthURL = @"/merchant/register"; //认证接口
static NSString *const kResAuthAgainURL = @"/merchant/modify"; //修改后,再次认证接口

@interface LZRestaurantAuthPresenter()

@end

@implementation LZRestaurantAuthPresenter

#pragma mark -
#pragma mark 认证商家

/**
 商家认证
 
 @param authModel <#authModel description#>
 @param successBlokc <#successBlokc description#>
 @param failureBlock <#failureBlock description#>
 */
- (void)authRestaurantWithModel:(LZRestaurantAuthModel *)authModel Success:(PresenterCommonSuccess)successBlokc Failure:(PresenterCommonFail)failureBlock
{
    NSString *urlStr = COMBINATION(merchant,kResAuthURL);
    NSLog(@"%@", urlStr);
    
    NSMutableDictionary *dic = authModel.mj_keyValues;
    [dic setObject:[LZUserModel Instance].userToken forKey:@"token"];
    
    if ([dic.allKeys containsObject:@"cookingStyleName"]) {
        [dic removeObjectForKey:@"cookingStyleName"];
    }
    
    if ([dic.allKeys containsObject:@"cityName"]) {
        [dic removeObjectForKey:@"cityName"];
    }
    
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        if ([LZBasePresenter isRequestStatusCode:resultData fail:failureBlock]) {
            if (successBlokc) {
                successBlokc(resultData);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failureBlock) {
            failureBlock(error.description);
        }
    }];
}

#pragma mark -
#pragma mark 商家认证被拒,修改后再次认证
- (void)authRestaurantAgainWithModel:(LZRestaurantAuthModel *)authModel Success:(PresenterCommonSuccess)successBlokc Failure:(PresenterCommonFail)failureBlock
{
    NSString *urlStr = COMBINATION(merchant,kResAuthAgainURL);
    NSLog(@"%@", urlStr);
    
    NSMutableDictionary *dic = authModel.mj_keyValues;
    [dic setObject:[LZUserModel Instance].userToken forKey:@"token"];
    
    if ([dic.allKeys containsObject:@"cookingStyleName"]) {
        [dic removeObjectForKey:@"cookingStyleName"];
    }
    
    if ([dic.allKeys containsObject:@"reviewComments"]) {
        [dic removeObjectForKey:@"reviewComments"];
    }
    
    if ([dic.allKeys containsObject:@"cityName"]) {
        [dic removeObjectForKey:@"cityName"];
    }
    
    [dic setObject:[NSNumber numberWithInteger:1] forKey:@"state"];//传待审核的状态值
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:dic success:^(NSURLSessionDataTask *task, id resultData) {
        if ([LZBasePresenter isRequestStatusCode:resultData fail:failureBlock]) {
            if (successBlokc) {
                successBlokc(resultData);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        if (failureBlock) {
            failureBlock(error.description);
        }
    }];

}

#pragma mark -
#pragma mark 获取菜系
/**
 获取菜系
 
 @param successBlock <#successBlock description#>
 @param failureBlock <#failureBlock description#>
 */
- (void)getRestaurantCuisineSuccess:(void(^)(NSArray *cuisineArr))successBlock Failure:(PresenterCommonFail)failureBlock
{
    NSString *urlStr = COMBINATION(merchant,kResAuthCuisineURL);
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:nil success:^(NSURLSessionDataTask *task, id resultData) {
        if ([LZBasePresenter isRequestStatusCode:resultData fail:failureBlock]) {
            self.restaurantCuisineArr = @[].mutableCopy;
            NSArray *arrTemp = resultData[@"data"];
            for (int i = 0; i < arrTemp.count; i ++) {
                LZRestaurantCuisineModel *m = [LZRestaurantCuisineModel mj_objectWithKeyValues:arrTemp[i]];
                [self.restaurantCuisineArr addObject:m];
            }
            
            if (successBlock) {
                successBlock(self.restaurantCuisineArr.copy);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}

/**
 基于已经选择的菜系数组获取用于展示到弹出框选择器的菜系数组
 
 @param cuisineSelectedArr 已经选择的菜系数组
 @return 用于弹出框选择器展示的数组
 */
- (NSArray *)getRestaurantCuisineArrForPopSelectViewBaseSelectedCuisineArr:(NSArray *)cuisineSelectedArr
{
    if (self.restaurantCuisineArr.count == 0) {
        return @[];
    }
      //先把菜系数组模型转换成用于展示的菜系模型
    NSMutableArray *showArr = @[].mutableCopy;
    for (int i = 0; i < self.restaurantCuisineArr.count; i ++) {
        LZRestaurantCuisineModel *m = self.restaurantCuisineArr[i];
        LZPopSelectCollectViewModel *selectModel = [[LZPopSelectCollectViewModel alloc] init];
        selectModel.contentID = m.cuisineId;
        selectModel.contentName = m.name;
        selectModel.isSelected = NO; //默认没有选中
        
        for (int j = 0; j < cuisineSelectedArr.count; j ++) {
            LZPopSelectCollectViewModel *baseModel = cuisineSelectedArr[j];
            if ([baseModel.contentID isEqualToString:selectModel.contentID]) {
                selectModel.isSelected = YES;
            }
        }
        
        [showArr addObject:selectModel];
    }
    
    return showArr.copy;
}


- (NSArray *)fillCookingStyleAndCallBackSelectedCookingStyleArrWhenAuthRegetcedWithAuthModel:(LZRestaurantAuthModel *)authModel
{
    if (self.restaurantCuisineArr.count == 0) {
        return @[].mutableCopy;
    }
    
    NSMutableArray *tempArr = [[NSMutableArray alloc] init];
    NSArray *cookingStyleIdArr = [authModel.cookingStyle componentsSeparatedByString:@","];
    
    //设置 用于展示弹出框用的数组
    for (int i = 0; i < self.restaurantCuisineArr.count; i ++) {
        LZRestaurantCuisineModel *m = self.restaurantCuisineArr[i];
        if ([cookingStyleIdArr containsObject:m.name]) {
            LZPopSelectCollectViewModel *popM = [[LZPopSelectCollectViewModel alloc] init];
            popM.contentName = m.name;
            popM.contentID = m.cuisineId;
            popM.isSelected = YES;
            [tempArr addObject:popM];
        }
    }
    
    NSString *cookingStyleName = @"";
    for (int i = 0; i < tempArr.count ; i ++) {
        LZPopSelectCollectViewModel *m = tempArr[i];
        if (i == 0) {
            cookingStyleName = m.contentName;
        }else{
            cookingStyleName = [NSString stringWithFormat:@"%@,%@",cookingStyleName,m.contentName];
        }
    }
    
    authModel.cookingStyleName = cookingStyleName;

    return tempArr.copy;
}

#pragma mark -
#pragma mark 获取热门商区
/**
 获取热门商区
 
 @param successBlock <#successBlock description#>
 @param failureBlock <#failureBlock description#>
 */
- (void)getHotAreaSuccess:(void(^)(NSArray *areaArr))successBlock Failure:(PresenterCommonFail)failureBlock
{
    NSString *urlStr = COMBINATION(merchant,kResAuthAreaURL);
    [[NetworkServiceManager manager] executePostTask:urlStr parameters:nil success:^(NSURLSessionDataTask *task, id resultData) {
        if ([LZBasePresenter isRequestStatusCode:resultData fail:failureBlock]) {
            self.restaurantAreaArr = @[].mutableCopy;
            NSArray *arrTemp = resultData[@"data"];
            for (int i = 0; i < arrTemp.count; i ++) {
                LZRestaurantAreaModel *m = [LZRestaurantAreaModel mj_objectWithKeyValues:arrTemp[i]];
                [self.restaurantAreaArr addObject:m];
            }
            
            if (successBlock) {
                successBlock(self.restaurantAreaArr.copy);
            }
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];

}

/**
 基于已经选择的商区的数组获取用于展示到弹出框选择器的商区数组
 
 @param areaSelectedArr 已经选择的商区数组
 @return 用于展示的商区数组
 */
- (NSArray *)getRestaurantAreaArrForPopSelectViewBaseSelectedAreaArr:(NSArray *)areaSelectedArr
{
    if (self.restaurantAreaArr.count == 0) {
        return @[];
    }
    //先把菜系数组模型转换成用于展示的菜系模型
    NSMutableArray *showArr = @[].mutableCopy;
    for (int i = 0; i < self.restaurantAreaArr.count; i ++) {
        LZRestaurantAreaModel *m = self.restaurantAreaArr[i];
        LZPopSelectCollectViewModel *selectModel = [[LZPopSelectCollectViewModel alloc] init];
        selectModel.contentID = m.areaId;
        selectModel.contentName = m.name;
        selectModel.isSelected = NO; //默认没有选中
        
        for (int j = 0; j < areaSelectedArr.count; j ++) {
            LZPopSelectCollectViewModel *baseModel = areaSelectedArr[j];
            if ([baseModel.contentID isEqualToString:selectModel.contentID]) {
                selectModel.isSelected = YES;
            }
        }
        
        [showArr addObject:selectModel];
    }
    
    return showArr.copy;
}

@end
